<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
  })
  interface formQuert {
    white_id:number
    user_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, trigger: 'blur', message: '请输入用户UUID' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      equipApi.whiteUserAdd(form).then(res=>{
        loading.close()
        ToastSuccess('新增白名单配置成功')
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      white_id='',
    } = query
    action.title = '新增用户白名单配置'
    Object.assign(form,{
      white_id,
      user_id:'',
    })
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID:" prop="user_id">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>